package com.thuanpq60023.easyenglish.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.thuanpq60023.easyenglish.R;
import com.thuanpq60023.easyenglish.bus.UnitBus;
import com.thuanpq60023.easyenglish.bus.VocabularyBus;
import com.thuanpq60023.easyenglish.entity.UnitEntity;
import com.thuanpq60023.easyenglish.entity.VocabEntity;
import com.thuanpq60023.easyenglish.view.ActionItem;
import com.thuanpq60023.easyenglish.view.Header;
import com.thuanpq60023.easyenglish.view.QuickAction;

public class ListActivity extends Activity {
	private ListView mListView;
	private VocabularyBus mEasyEnglishTBBus;
	private UnitBus mUnitBus;
	private ListActivityAdapter mActivityAdapter;
	private Spinner spnUnit;
	private ProgressDialog mProgressDialog;
	private VocabEntity temp;

	/** Test Popup **/
	// action id
	private static final int ID_MEAN = 1;
	private static final int ID_DELETE = 2;
	private static final int ID_EDIT = 3;
	private static final int ID_CANCEL = 4;
	private QuickAction quickAction;

	/** Test Popup **/

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		/** Test Popup **/
		ActionItem item_mean = new ActionItem(ID_MEAN, "Mean", getResources()
				.getDrawable(R.drawable.ic_mean));
		ActionItem item_edit = new ActionItem(ID_EDIT, "Edit", getResources()
				.getDrawable(R.drawable.ic_edit));
		ActionItem item_delete = new ActionItem(ID_DELETE, "Delete",
				getResources().getDrawable(R.drawable.ic_delete));
		ActionItem item_cancel = new ActionItem(ID_CANCEL, "Cancel",
				getResources().getDrawable(R.drawable.ic_cancel));

		quickAction = new QuickAction(this, QuickAction.HORIZONTAL);
		quickAction.addActionItem(item_mean);
		quickAction.addActionItem(item_edit);
		quickAction.addActionItem(item_delete);
		quickAction.addActionItem(item_cancel);
		quickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction source, int pos,
							int actionId) {
						ActionItem actionItem = quickAction.getActionItem(pos);
						// here we can filter which action item was clicked with
						// pos or actionId parameter
						switch (actionId) {
						case ID_MEAN:
							showMean();
							break;
						case ID_EDIT:
							editWord();
							break;
						case ID_DELETE:
							deleteWord();
							break;
						default:
							break;
						}
					}
				});

		/** Test Popup **/
		intiChild();
		initView();
		initHeader();
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadVocab();
	}

	private void intiChild() {
		mEasyEnglishTBBus = new VocabularyBus(ListActivity.this);
		mUnitBus = new UnitBus(ListActivity.this);
		mProgressDialog = new ProgressDialog(ListActivity.this);
		mProgressDialog.setMessage("Loading...");
		mProgressDialog.setCancelable(false);
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.list_layout_lst);
		spnUnit = (Spinner) findViewById(R.id.list_layout_spn_unit);

		UnitEntity[] units = mUnitBus.getAllUnit();
		String[] unitnames = new String[units.length];
		for (int i = 0; i < units.length; i++) {
			unitnames[i] = units[i].getName();
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, unitnames);
		adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
		spnUnit.setAdapter(adapter);
		spnUnit.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				loadVocab();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				temp = (VocabEntity) arg0.getItemAtPosition(arg2);
				quickAction.show(arg1);
			}
		});
	}

	private Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			mProgressDialog.dismiss();
		}
	};

	public void initHeader() {
		Header header = new Header(this);
		header.setTextTitle(getString(R.string.list_layout));
		header.getImvMenu().setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void deleteWord() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Are you sure?");
		dialog.setMessage(temp.getWord());
		dialog.setIcon(android.R.drawable.ic_dialog_alert);
		dialog.setCancelable(true);
		dialog.setPositiveButton("Yes", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mEasyEnglishTBBus.deleteVocabByWordAndType(temp.getWord(),
						temp.getType());
				loadVocab();
			}
		});

		dialog.setNegativeButton("Cancel", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog.create();
		dialog.show();
	}

	private void editWord() {
		Intent intent = new Intent();
		intent.setClass(ListActivity.this, ImportActivity.class);
		intent.putExtra("Word", temp.getWord());
		intent.putExtra("Type", temp.getType());
		Log.e("ThuanPQ",
				"Word - Type: " + temp.getWord() + " - " + temp.getType());
		startActivity(intent);
		finish();
	}

	private void showMean() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle(temp.getWord());
		dialog.setMessage(temp.getMean());
		dialog.setIcon(android.R.drawable.ic_dialog_alert);
		dialog.setCancelable(true);
		dialog.setPositiveButton("Close", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog.create();
		dialog.show();
	}

	private void loadVocab() {
		mProgressDialog.show();
		new SetListviewData().execute(null);
	}

	class SetListviewData extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				if (spnUnit.getCount() > 0) {
					VocabEntity[] mEntities = mEasyEnglishTBBus
							.getVocabByUnit(spnUnit.getSelectedItem()
									.toString());
					mActivityAdapter = new ListActivityAdapter(mEntities,
							ListActivity.this);
					Log.e("ThuanPQ", Integer.toString(mEntities.length));
				}
			} catch (Exception e) {
				Log.e("ThuanPQ", e.getMessage().toString());
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			mListView.setAdapter(mActivityAdapter);
			handler.sendEmptyMessage(0);
		}
	}
}
